package com.ztmg.water.service.base.impl;

import com.ztmg.water.model.base.DeliveryMan;
import com.ztmg.water.vo.base.DeliveryManVO;
import com.ztmg.water.dto.base.DeliveryManDTO;
import com.ztmg.water.dto.base.DeliveryManPageDTO;
import com.ztmg.common.util.resultUtils.JsonResult;
import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ztmg.common.constant.ErrorConst;
import org.springframework.beans.BeanUtils;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import com.ztmg.water.service.base.DeliveryManService;
import com.ztmg.water.mapper.base.DeliveryManMapper;import com.ztmg.water.model.base.DeliveryManExample;

@Service
public class DeliveryManServiceImpl implements DeliveryManService{

    @Resource
    private DeliveryManMapper deliveryManMapper;
    @Override
    public JsonResult create(DeliveryManDTO dto) {
        if(null != dto){
            DeliveryMan bean = new DeliveryMan();
            BeanUtils.copyProperties(dto,bean);
            bean.setCreateTime(new Date());
            //后续完善
            bean.setCreater("");
            deliveryManMapper.insert(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult update(DeliveryManDTO dto) {
        if(null != dto && null != dto.getId()){
            DeliveryMan bean = new DeliveryMan();
            BeanUtils.copyProperties(dto,bean);
            bean.setModifyTime(new Date());
            //后续完善
            bean.setModifier("");
            deliveryManMapper.updateByPrimaryKeySelective(bean);
            return JsonResult.success();
        }else {
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<DeliveryManVO> selectById(Integer id) {
        if(null != id){
            DeliveryMan bean = deliveryManMapper.selectByPrimaryKey(id);
            DeliveryManVO vo = new DeliveryManVO();
            if(null != bean){
                BeanUtils.copyProperties(bean,vo);
            }else {
                return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
            }
            return JsonResult.success(vo);
        }else{
            return JsonResult.fail(ErrorConst.SYSTEM_PARM_ERROR,ErrorConst.SYSTEM_PARM_ERROR_MSG);
        }
    }

    @Override
    public JsonResult<List<DeliveryManVO>> select(DeliveryManDTO dto) {
        DeliveryManExample example = new DeliveryManExample();
        DeliveryManExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        List<DeliveryMan> sysUserList = deliveryManMapper.selectByExample(example);
        List<DeliveryManVO> respDTOS = sysUserList.parallelStream().map((bean) -> {
            DeliveryManVO vo = new DeliveryManVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        return JsonResult.success(respDTOS);
    }

    @Override
    public JsonResult<PageInfo<DeliveryManVO>> selectPage(DeliveryManPageDTO dto) {
        DeliveryManExample example = new DeliveryManExample();
        DeliveryManExample.Criteria criteria =  example.createCriteria();
        if(StringUtils.isNotEmpty(dto.getSearchKey())){
            //根据送水人员名称查询
            criteria.andDeliveryNameLike("%"+dto.getSearchKey()+"%");
        }
        PageHelper.startPage(dto.getPage(), dto.getLimit());
        List<DeliveryMan> beanList = deliveryManMapper.selectByExample(example);
        PageInfo pageInfo = new PageInfo<>(beanList);
        List<DeliveryManVO> respDTOS = beanList.parallelStream().map((bean) -> {
            DeliveryManVO vo = new DeliveryManVO();
            BeanUtils.copyProperties(bean, vo);
            return vo;
        }).collect(Collectors.toList());
        pageInfo.setList(respDTOS);
        return JsonResult.success(pageInfo);
    }

    @Override
    public JsonResult deleteById(Integer id) {
        return JsonResult.success( deliveryManMapper.deleteByPrimaryKey(id));
    }

    @Override
    public JsonResult delete(DeliveryManDTO dto) {
        DeliveryManExample example = new DeliveryManExample();
        DeliveryManExample.Criteria criteria =  example.createCriteria();
        /***
        * 根据具体的业务，查询条件赋值
         */
        return JsonResult.success(deliveryManMapper.deleteByExample(example));
    }

    @Override
    public JsonResult batchDelete(List<Integer> ids) {
        DeliveryManExample example = new DeliveryManExample();
        DeliveryManExample.Criteria criteria =  example.createCriteria();
        criteria.andIdIn(ids);
        return JsonResult.success(deliveryManMapper.deleteByExample(example));
    }
}